export default class EngineUtil {
    //获取预制体资源
    public static GetResPrefab(name: string, url: string): any {
        return cc.loader.getRes("prefab/" + url + "/" + name, cc.Prefab);
    }

    //获取Json配置
    public static GetResConfig(name: string): any {
        return cc.loader.getRes("config/" + name, cc.Asset);
    }

    //获取MP3资源
    public static GetResAudio(audio: string): any {
        if (audio) {
            let audioClip = cc.loader.getRes("audio/" + audio);
            if (audioClip) {
                return audioClip;
            }
            else {
                cc.loader.loadRes("audio/" + audio, cc.AudioClip, function (err, clip) {
                    return clip;
                });
            }
        }
    }

    public static SetAtlasTexture(node: cc.Node, atlasname: string, filename: string) {
        if (node && atlasname && filename) {
            let atlas: cc.SpriteAtlas = cc.loader.getRes(atlasname, cc.SpriteAtlas);
            if (atlas) {
                let spriteFrame = atlas.getSpriteFrame(filename);
                if (spriteFrame) {
                    let sprite = node.getComponent(cc.Sprite);
                    if (sprite) {
                        sprite.spriteFrame = spriteFrame;
                    }
                }
            }
        }
    }

    //设置精灵节点的图片
    public static SetNodeTexture(node: cc.Node, file: string, filename: string): any {
        if (node && filename) {
            let strName = "texture/" + file + "/" + filename;
            let tmpTexture = cc.loader.getRes(strName);
            if (tmpTexture) {
                let sprite = node.getComponent(cc.Sprite);
                if (sprite) {
                    sprite.spriteFrame = new cc.SpriteFrame(tmpTexture);
                }
            } else {
                cc.loader.loadRes(strName, function (err, texture) {
                    let sprite = node.getComponent(cc.Sprite);
                    if (sprite) {
                        sprite.spriteFrame = new cc.SpriteFrame(texture);
                    }
                });
            }
        }
    }

    //设置图片帧
    public static SetNodeSpriteFrame(node: cc.Node, spriteFrame: cc.SpriteFrame): void {
        let sprite = node.getComponent(cc.Sprite);
        if (sprite) {
            sprite.spriteFrame = spriteFrame;
        }
    }

    //设置文本节点的内容
    public static SetNodeText(node: cc.Node, text: string): void {
        if (node) {
            let label = node.getComponent(cc.Label);
            if (label) {
                label.string = text;
            }
        }
    }

    public static CreateSprite(name: string, texture: cc.Texture2D): cc.Node {
        let node = new cc.Node(name);
        if (node) {
            let sprite = node.addComponent(cc.Sprite);
            sprite.spriteFrame = new cc.SpriteFrame(texture);
        }
        return node;
    }

    public static CreateSpriteWithFrame(name: string, spriteFrame: cc.SpriteFrame): cc.Node {
        let node = new cc.Node(name);
        if (node) {
            let sprite = node.addComponent(cc.Sprite);
            sprite.spriteFrame = spriteFrame;
            return node;
        }
        return null;
    }

    //获取指定节点的指定组件
    public static GetComponent<T extends cc.Component>(node: cc.Node, component: any): T {
        if (node && component) {
            return node.getComponent(component);
        }
        return null;
    }

    //添加组件
    public static AddComponent(node: cc.Node, component: any): cc.Component {
        if (node && component) {
            return node.addComponent(component);
        }
        return null;
    }

    //移除组件
    public static RemoveComponent(node: cc.Node, component: any): void {
        if (node && component) {
            node.removeComponent(component);
        }
    }

    public static SetWidgetTarget(node: cc.Node, target: any): void {
        if (node && target) {
            let widgetCom = node.getComponent(cc.Widget);
            if (widgetCom) {
                widgetCom.target = target;
            }
        }
    }

    public static SetNodeDragonBones(node: cc.Node, file: string, name: string) {
        var dragonDisplay = node.getComponent(dragonBones.ArmatureDisplay);
        if (!dragonDisplay) return;

        let atlasstr = "effect/" + file + "/" + name + "_tex";
        let dragon = "effect/" + file + "/" + name + "_ske";

        var atlas = cc.loader.getRes(atlasstr, dragonBones.DragonBonesAtlasAsset);
        var asset = cc.loader.getRes(dragon, dragonBones.DragonBonesAsset);
        dragonDisplay.dragonAtlasAsset = atlas;
        dragonDisplay.dragonAsset = asset;
        dragonDisplay.armatureName = "armatureName";
    }

    public static PlayAnimation(node: cc.Node, strAct: string, nTimes: number, callBack: Function = null) {
        var dragonDisplay: dragonBones.ArmatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
        if (dragonDisplay) {
            dragonDisplay.playAnimation(strAct, nTimes);
            dragonDisplay.addEventListener(dragonBones.EventObject.COMPLETE, () => {
                if (callBack) {
                    callBack();
                }
            });
        }
    }

    public static SetNodeSpine(node: cc.Node, file: string, name: string) {
        var spSkeleton = node.getComponent(sp.Skeleton);
        if (!spSkeleton) return;

        let str = "texture/" + file + "/" + name;
        var spineData = cc.loader.getRes(str, sp.SkeletonData);
        spSkeleton.skeletonData = spineData;
    }

    public static setGray(node: cc.Node, boo: boolean) { //false: 不置灰  true: 置灰
        var Sprite = node.getComponent(cc.Sprite);
        let matherial = boo ? "2d-gray-sprite" : "2d-sprite"
        Sprite.setMaterial(0, cc.Material.getBuiltinMaterial(matherial));
    }
}

